﻿<UserControl x:Class="LINQPad.OpenAccess.UI.View.BasicSettingsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:conv="clr-namespace:LINQPad.OpenAccess.UI.Converters"             
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"             
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             MinWidth="650" MinHeight="350"
             d:DesignHeight="350" d:DesignWidth="650">
    <UserControl.Resources>
        <!-- converter -->
        <conv:NegatedBooleanToVisibilityConverter x:Key="NegatedBool2Vis" />
        <conv:NegateBooleanConverter x:Key="negateBool" />
        <!-- templates -->
        <DataTemplate x:Key="ContextTypeItemTemplate">
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
        <DataTemplate x:Key="MetadataSourceItemTemplate">
            <TextBlock >
                    <Run Text="{Binding Name, Mode=OneWay}" /> <Run Text="{Binding MappingType, Mode=OneWay, StringFormat={}({0})}"/>
            </TextBlock>
        </DataTemplate>
        <!-- collection view sources -->
        <CollectionViewSource x:Key="ContextTypesSource" Source="{Binding ContextTypes}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="ContextType.Namespace" />
            </CollectionViewSource.GroupDescriptions>
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="ContextType.Namespace" />
                <scm:SortDescription PropertyName="Name" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
        <CollectionViewSource x:Key="MetadataSourcesSource" Source="{Binding MetadataSources}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Path" />
            </CollectionViewSource.GroupDescriptions>
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Path" />
                <scm:SortDescription PropertyName="Name" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </UserControl.Resources>
    <DockPanel>
        <!-- choose assembly -->
        <DockPanel DockPanel.Dock="Top">
            <TextBlock DockPanel.Dock="Right" Foreground="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}">
                <Hyperlink Command="{Binding RefreshCommand}">Refresh</Hyperlink> | <Hyperlink Command="{Binding ChooseAssemblyCommand}">Browse...</Hyperlink>
            </TextBlock>
            <TextBlock Text="Path to custom assembly:" />
        </DockPanel>
        <TextBox DockPanel.Dock="Top" Margin="5 2" Style="{DynamicResource ValidatableReadOnlyTextBoxStyle}" 
                 Text="{Binding AssemblyFilePath, ValidatesOnDataErrors=True}" />
        <!-- choose app.config -->
        <DockPanel DockPanel.Dock="Top">
            <TextBlock DockPanel.Dock="Right">
                <Hyperlink Command="{Binding ChooseAppConfigCommand}">Browse...</Hyperlink>
            </TextBlock>
            <TextBlock Text="Path to custom app.config:" />
        </DockPanel>
        <TextBox DockPanel.Dock="Top" Margin="5 2" Style="{DynamicResource ValidatableReadOnlyTextBoxStyle}"
                 Text="{Binding AppConfigFilePath, ValidatesOnDataErrors=True}" />
        <!-- choose context type and metadata source -->
        <UniformGrid Columns="2">
            <GroupBox Header="Context types">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <ListBox Grid.Row="0" 
                            ItemsSource="{Binding Source={StaticResource ContextTypesSource}}" 
                            SelectedItem="{Binding SelectedContextType,Mode=TwoWay, ValidatesOnDataErrors=True}"
                            ItemTemplate="{StaticResource ContextTypeItemTemplate}">
                        <ListBox.GroupStyle >
                            <GroupStyle />
                        </ListBox.GroupStyle>
                    </ListBox>
                    <TextBlock Grid.Row="0"
                               Text="No context types available..." Margin="10 5" Foreground="Red"
                               Style="{DynamicResource ErrorHintTextBlockStyle}" 
                               Visibility="{Binding HasContextTypes, Converter={StaticResource NegatedBool2Vis}}"/>
                    <CheckBox Margin="0 2" Grid.Row="1"
                              IsChecked="{Binding UseDefaultConstructor, Mode=TwoWay}"
                              IsEnabled="{Binding CanUseDefaultConstructor}"
                              Content="Use default constructor" />
                </Grid>
            </GroupBox>
            <GroupBox Header="Metadata sources">
                <Grid IsEnabled="{Binding UseDefaultConstructor, Converter={StaticResource negateBool}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <CheckBox Grid.Row="0" Margin="0 2"
                              IsChecked="{Binding UseDefaultMetadataSource, Mode=TwoWay}" Content="Use default metadata source" 
                              IsEnabled="{Binding CanUseDefaultMetadataSource}"/>
                    <ListBox Grid.Row="1"
                             ItemsSource="{Binding Source={StaticResource MetadataSourcesSource}}" 
                             SelectedItem="{Binding SelectedMetadataSource,Mode=TwoWay, ValidatesOnDataErrors=True}"
                             ItemTemplate="{StaticResource MetadataSourceItemTemplate}"
                             IsEnabled="{Binding UseDefaultMetadataSource, Converter={StaticResource negateBool}}">
                        <ListBox.GroupStyle >
                            <GroupStyle />
                        </ListBox.GroupStyle>
                    </ListBox>
                    <TextBlock Grid.Row="1" Text="No metadata sources available..." Margin="10 5" Foreground="Red"
                               Style="{DynamicResource ErrorHintTextBlockStyle}" 
                               Visibility="{Binding HasMetadataSources, Converter={StaticResource NegatedBool2Vis}}"/>
                </Grid>
            </GroupBox>
        </UniformGrid>
    </DockPanel>
</UserControl>
